package com.zhouyu.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.zhouyu.service.ZhouyuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.LocaleResolver;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Date;

@RestController
public class ZhouyuController {

	@Autowired
	private ZhouyuService zhouyuService;

	@Resource
	private ApplicationContext applicationContext;

	@InitBinder("zhouyuService")
	public void initBinder(ZhouyuService zhouyuService) {
		this.zhouyuService = zhouyuService;
	}
	public void initBinder2(ZhouyuService zhouyuService) {
		this.zhouyuService = zhouyuService;
	}
	@ModelAttribute
	public void init(ModelMap modelMap) {
		modelMap.put("a", "aaaaaa");
		modelMap.put("b", "bbbbb");
	}

	@GetMapping("/test")
	public String test( String name, @ModelAttribute String age, @ModelAttribute String age2, ModelMap map) {
		LocaleResolver localeResolver = applicationContext.getBean("localeResolver", LocaleResolver.class);
		LocaleResolver localeResolver1 = applicationContext.getAutowireCapableBeanFactory().getBean("localeResolver", LocaleResolver.class);
		System.out.println(localeResolver != null);
		System.out.println(localeResolver1 != null);
		return zhouyuService.test();
	}
	@GetMapping("/test2")
	public String test(@Validated  @NotNull(message = "主计划id必传") String id, @Validated @NotNull(message = "主计划id必传")  @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd") Date deliveryDate) {
		return zhouyuService.test();
	}

}
